unit MainFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, MPlayer, mmSystem, WaveUtils, WaveStorage, WaveIO, WaveIn,
  WaveRecorders, StdCtrls, WaveOut, WavePlayers, SpeechMatchFeedback,
  SpeechRecognitionFeedback, SpeechRepeat, ExtCtrls;

type
  TForm1 = class(TForm)
    btnStartReco: TButton;
    btnendreco: TButton;
    btnStartMatch: TButton;
    btnEndMatch: TButton;
    btnstartRepeat_1: TButton;
    btnstartRepeat_2: TButton;
    btnendRepeat: TButton;
    mmlog: TMemo;
    Timer1: TTimer;
    procedure btnEndMatchClick(Sender: TObject);
    procedure btnStartRecoClick(Sender: TObject);
    procedure btnendrecoClick(Sender: TObject);
    procedure btnendRepeatClick(Sender: TObject);
    procedure btnStartMatchClick(Sender: TObject);
    procedure btnstartRepeat_1Click(Sender: TObject);
    procedure btnstartRepeat_2Click(Sender: TObject);
  private
    FMatchFeedback: TSpeechMatchFeedback;
    FReco: TSpeechRecognitionFeedback;
    FRepeat: TSpeechRepeat;
    // reco
    procedure ResponseReco(AAnswer: string; ADuration: Integer);
    procedure ResponseEnd(ADuration: Integer);
    procedure ResponseTimeout();
    // match
    procedure ResponseMatch(ADegree: Integer);
    procedure ResponseMatchEnd(ADuration: Integer);
    procedure ResponseMatchTimeout();
    // repeat
    procedure ResponseRepeatEnd(ADuration: Integer);
  public

  end;

var
  Form1: TForm1;

implementation

uses
  CommonUtils;

{$R *.dfm}

procedure TForm1.btnEndMatchClick(Sender: TObject);
begin
  FMatchFeedback.Stop;
  FreeAndNil(FMatchFeedback);
end;

procedure TForm1.btnStartRecoClick(Sender: TObject);
begin
  FReco := TSpeechRecognitionFeedback.Create(Self, nil);
  FReco.Answer.Add('about');
  FReco.Answer.Add('above');
  FReco.Answer.Add('a');
  FReco.Timeout := 10000;
  FReco.OnMatch := ResponseReco;
  FReco.OnTimeout := ResponseTimeout;
  FReco.OnEnd := ResponseEnd;
  FReco.Start;
end;

procedure TForm1.btnendrecoClick(Sender: TObject);
begin
  FReco.Stop;
  FreeAndNil(FReco);
end;

procedure TForm1.btnendRepeatClick(Sender: TObject);
begin
  FRepeat.Stop;
  FreeAndNil(FRepeat);
end;

procedure TForm1.btnStartMatchClick(Sender: TObject);
begin
  FMatchFeedback := TSpeechMatchFeedback.Create;
  FMatchFeedback.Timeout := 5000;
  FMatchFeedback.Answer := GetExePath + 'about.wav';
  FMatchFeedback.OnMatch := ResponseMatch;
  FMatchFeedback.OnEnd := ResponseMatchEnd;
  FMatchFeedback.OnTimeout := ResponseMatchTimeout;
  FMatchFeedback.Start;
end;

procedure TForm1.btnstartRepeat_1Click(Sender: TObject);
begin
  FRepeat := TSpeechRepeat.Create;
  FRepeat.Mode := 1;
  FRepeat.Delay := 1000;
  FRepeat.OnEnd := ResponseRepeatEnd;
  FRepeat.Start;
end;

procedure TForm1.btnstartRepeat_2Click(Sender: TObject);
begin
  FRepeat := TSpeechRepeat.Create;
  FRepeat.Mode := 2;
  FRepeat.Delay := 1000;
  FRepeat.OnEnd := ResponseRepeatEnd;
  FRepeat.Start;
end;

procedure TForm1.ResponseEnd(ADuration: Integer);
begin
  mmlog.Lines.Add('Reco End: ADuration=' + IntToStr(ADuration));
end;

procedure TForm1.ResponseMatch(ADegree: Integer);
begin
  mmlog.Lines.Add('Match: ADegree=' + IntToStr(ADegree));
end;

procedure TForm1.ResponseMatchEnd(ADuration: Integer);
begin
  mmlog.Lines.Add('Match end: ADuration=' + IntToStr(ADuration));
end;

procedure TForm1.ResponseMatchTimeout;
begin
  mmlog.Lines.Add('Match timeout');
end;

procedure TForm1.ResponseReco(AAnswer: string; ADuration: Integer);
begin
  mmlog.Lines.Add('Reco Answer: Answer=' + AAnswer + ' ADuration=' + IntToStr(ADuration));
end;

procedure TForm1.ResponseRepeatEnd(ADuration: Integer);
begin
  mmlog.Lines.Add('Repeat end: ADuration=' + IntToStr(ADuration));
end;

procedure TForm1.ResponseTimeout;
begin
  mmlog.Lines.Add('Reco timeout');
end;

end.
